package com.yootk.gateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

@Slf4j
@Component
public class LogGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {// 日志过滤工厂类
    @Override
    public GatewayFilter apply(NameValueConfig config) { // 过滤处理
        return (exchange, chain) -> { // 编写具体的过滤实现
            ServerHttpRequest request = exchange.getRequest().mutate().build();
            ServerWebExchange webExchange = exchange.mutate().request(request).build();
            log.info("配置参数：{}，{}", config.getName(), config.getValue()); // application.yml
            log.info("请求路径：{}、请求模式：{}", request.getPath(), request.getMethod());
            return chain.filter(webExchange); // 向下执行
        };
    }

}
